package com.xhapi.messageQueue.consumer;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhapi.pojo.entity.HistoryQuestionByAi;
import com.xhapi.service.ESService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class Consumer {
    @Autowired
    ESService esService;
    @RabbitListener(queues = "writeInEsQueue")
    public void handlerMessage(String message){
        log.info("接收到消息：{}",message);
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            List<HistoryQuestionByAi>list=objectMapper.readValue(message, new TypeReference<List<HistoryQuestionByAi>>() {});

            esService.addES(list);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
